package ui.list 
{
	import com.GCInventory;
	import com.GCItem;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author Jason Won
	 */
	public class GCSalesScrollList extends ScrollList
	{	
		private var _inventory:GCInventory;					// Inventory currently connected to this list		
		
		/**
		 * Constructor
		 * @param	width			Width of the scroll list
		 * @param	height			Height of the scroll list
		 */
		public function GCSalesScrollList(width:int, height:int, inventory:GCInventory = null) 
		{
			super(width, height);
			_inventory = inventory;
			loadInventory(inventory);
		}		
		
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTIONS
		//----------------------------------------------------------------------		
		
		/**
		 * Loads the sales in the given inventory into this list
		 * @param	inventory
		 */
		public function loadInventory(inventory:GCInventory):void
		{
			// Loading a new inventory; Remove all previous sales
			removeAll();
			_inventory = inventory;
			_displayList = new Dictionary();
			
			// Fill the list with all the sales in the inventory
			if (_inventory != null)
			{
				var sales:GCItem;
				var salesUI:GCListUI;
				if (_inventory.inventory != null)
				{
					for each (sales in _inventory.inventory)
					{
						salesUI = new GCListUI(sales);
						addDisplay(salesUI);
						_displayList[sales] = salesUI;						
					}
				}
			}			
		}
		
		/**
		 * Adds the given sales to the list
		 * @param	newSale
		 */
		public function addSales(newSale:GCItem):void
		{
			// Check for the sale in the inventory
			if (_inventory != null)
			{
				var sales:Dictionary = _inventory.inventory;
				var sale:GCItem = sales[newSale.invItemId]
				
				if (sale != null)
				{
					// It exists --> this shouldn't happen
					// We will toss away the new sale for now
					return;
				}
				else 
				{
					// Sale doesn't exist; Add it to our list
					sales[newSale.invItemId] = newSale;
					var newSaleUI:GCListUI = new GCListUI(newSale);
					_displayList[newSale] = newSaleUI;
					addDisplay(newSaleUI);
				}
			}
			else
			{
				// We don't have an inventory
				return; // Or should we add to visual anyway?
			}
		}
		
		/**
		 * Removes the given sales from the list
		 * @param	sale
		 */
		public function removeSale(sale:GCItem):void
		{
			if (sale == null)
				return;
			var inventory:Dictionary = _inventory.inventory;
			if (_inventory != null)
			{
				// Check that the sale is in the inventory
				if (inventory[sale.invItemId] == null)
				{
					// Item doesn't exist; exit
					return;
				}
				else
				{
					// Item does exist; Remove item from the inventory
					delete inventory[sale.invItemId];
				}
				
				// Check for the display
				var saleUI:GCListUI = _displayList[sale];
				if (saleUI == null)
				{
					// Display doesn't exist; exit
					return;
				}
				else 
				{
					// Display exists; Remove the display
					removeDisplay(saleUI);
					delete _displayList[sale];
				}
			}
		}
		
		/**
		 * Redraws the list
		 */
		public function redraw():void
		{
			loadInventory(_inventory);
		}		
		
		/**
		 * Clears out all sales from the list
		 */
		override public function removeAll():void
		{
			super.removeAll();
			_inventory = null;
			_displayList = null;
			_selected = null;
		}
		
		//----------------------------------------------------------------------
		//	PRIVATE FUNCTIONS
		//----------------------------------------------------------------------				
		
		/**
		 * Adds the given sales UI to the list
		 * @param	newSalesUI
		 */
		private function addDisplay(newSalesUI:GCListUI):void
		{
			// XY Positioning
			newSalesUI.x = verticalPadding;
			newSalesUI.y = size * (verticalPadding + itemHeight);
			
			// Add listeners to the UI
			newSalesUI.addEventListener(MouseEvent.ROLL_OVER, handleRollOver);
			newSalesUI.addEventListener(MouseEvent.ROLL_OUT, handleRollOut);
			newSalesUI.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			newSalesUI.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			
			// Update the list properties
			size++;
			listHeight = size * (verticalPadding + itemHeight);// - hitAreaHeight;
			
			// Add the UI to the display
			_list.addChild(newSalesUI);			
		}
		
		/**
		 * Removes the given salesUI from the list
		 * @param	salesUI
		 */
		private function removeDisplay(salesUI:GCListUI):void
		{
			if (salesUI == null)
				return;
			
			// Check that the item is actually in the list
			if (salesUI.parent != _list)
				return;
				
			// Remove the listeners on the item
			salesUI.removeEventListener(MouseEvent.ROLL_OVER, handleRollOver);
			salesUI.removeEventListener(MouseEvent.ROLL_OUT, handleRollOut);
			salesUI.removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			salesUI.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			
			// Remove the UI from the display
			_list.removeChild(salesUI);
			
			// Update the list properties
			size--;
			listHeight = size * (verticalPadding + itemHeight);// - hitAreaHeight;
			
			// Unselect the sale
			_selected = null;
			
			// Redraw the list
			realign();			
		}
		
		
		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------

		/*
		override protected function handleMouseDown(event:MouseEvent):void
		{
			super.handleMouseDown(event);
			
			var object:GCListUI = event.currentTarget as GCListUI;
			
			// Show that the item is selected
			_selected = object;
		}
		
		override protected function handleMouseUp(event:MouseEvent):void
		{
			super.handleMouseUp(event);

		}*/
		
	}
}